<?php
//后台登陆控制
include_once('admin_global.php');
$db->Get_user_shell_check($uid, $shell);   //检测用户是否登录

//查询出一级导航栏目，存入数组
$query=$db->select("category","*","catePID=0");
//存储到数组
while($row=$db->fetch_array()){
    $nav[]=$row;
}
//分页显示文章
$page_size=12;   //每页显示多少篇文章
if(isset($_GET['page'])){
    $pageCurrent=$_GET['page'];
}else{
    $pageCurrent=1;
}
$subPage_link="listNews.php?page=";

$userName=$_SESSION['userName']; //获取登录用户名称

//根据栏目ID查询文章
if(isset($_GET['cateID'])){
    $cateID=$_GET['cateID'];
    //判断它是否有子栏目
    $query=$db->select("category","*","catePID=$cateID");
    $num=$db->db_num_rows();
    if($num>0){
        //有子栏目，查询出子栏目下的所有文章
        $sql="SELECT a.newsID,a.newsTitle,a.newsAuthor,a.newsSource,a.newsSendTime,a.newsUpdateTime,a.newsHits,a.isTop,a.isRed,b.id,b.cateName FROM news AS a,category AS b WHERE b.catePID=$cateID AND a.newsCID=b.id AND a.newsAuthor='$userName' ORDER BY a.newsSendTime DESC";
        $query=$db->query($sql);
        $sql_page=$sql . " LIMIT " . ($pageCurrent - 1) * $page_size . "," . $page_size;
        $query_page=$db->query($sql_page);
    }else{
        //没有子栏目，查询出该栏目下的所有文章
        $sql="SELECT a.newsID,a.newsTitle,a.newsAuthor,a.newsSource,a.newsSendTime,a.newsUpdateTime,a.newsHits,a.isTop,a.isRed,b.id,b.cateName FROM news AS a,category AS b WHERE b.id=$cateID AND a.newsCID=$cateID AND a.newsAuthor='$userName' ORDER BY a.newsSendTime DESC";
        $query=$db->query($sql);
        $sql_page=$sql . " LIMIT " . ($pageCurrent - 1) * $page_size . "," . $page_size;
        $query_page=$db->query($sql_page);
    }
}else{
    $cateID=0;    //如果没有传栏目ID则查询出数据库所有文章
    $sql = "SELECT a.newsID,a.newsTitle,a.newsAuthor,a.newsSource,a.newsSendTime,a.newsUpdateTime,a.newsHits,a.isTop,a.isRed,b.id,b.cateName FROM news AS a,category AS b WHERE b.id=a.newsCID AND a.newsAuthor='$userName' ORDER BY a.newsSendTime DESC";
    $query=$db->query($sql);
    $sql_page=$sql . " LIMIT " . ($pageCurrent - 1) * $page_size . "," . $page_size;
    $query_page=$db->query($sql_page);
}
//将查询出来的文章放入数组
//所有文章存储到$data数组
$data = array(); 
while($row=$query->fetch_array()){
    $data[]=$row;
}
//将分页查询结果存储到数组$newslist
$newslist = array(); 
while($row=$query_page->fetch_array()){
    $newslist[]=$row;
}
//计算出文章总数
$nums=count($data);
$sub_pages=$nums/$page_size+1;

//显示当前栏目位置
if($cateID==0){
    $cateName="";
}else{
    $query=$db->select("category","*","id=$cateID");
    while($row=$db->fetch_array()){
        $cateName=$row['cateName'];
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>文章管理</title>
        <link rel="stylesheet" href="css/right.css" type="text/css" />
        <script src="js/jquery-1.12.4.min.js" type="text/javascript"></script>
        <script src="js/bggl.js" type="text/javascript"></script> 
    </head>
    <script>
        function delNews()
        {
            if (window.confirm("文章删除后不能恢复，你确定要删除这篇文章吗？")){
                return true;
            } else{
                return false;
            }
        }
    </script>
    <body>
        <div id="right_main">
            <div class="right_content">
                <div class="right_top_menu">
                    <h3>
                        栏目导航：
                        <?php echo "| " ?>
                        <?php foreach($nav as $row){
                            echo " "."<a href=listNews.php?cateID=".$row['id'].">".$row['cateName']."</a>"." |";
                         }; ?>  
                    </h3>
                </div>
                <div class="right_center_content">
                    <form id="form1" method="post" action="">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td valign="top" >
                                    <div class="txl_search">&nbsp;&nbsp;您现在的位置：
                                    <?php if($cateID==0){; ?>
                                        文章管理 >> <a href="listNews.php">所有文章</a>
                                    <?php }else{; ?>
                                        文章管理 >> <a href="listNews.php">所有文章</a> >> <a href="listNews.php?cateID=<?php echo $cateID; ?>" ><?php echo $cateName; ?></a>
                                    <?php }; ?>
                                    </div>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" class="fg_table txl_table" id="table1">
                                        <tr>
                                            <th width="5%">ID</th>
                                            <th width="35%">文章标题</th>
                                            <th width="7%">作者</th>
                                            <th width="7%">来源</th>
                                            <th width="10%">发布时间</th>
                                            <th width="10%">修改时间</th>
                                            <th width="5%">浏览次数</th>
                                            <th width="5%">是否置顶</th>
                                            <th width="5%">是否加红</th>
                                            <th width="3%">修改</th>
                                            <th width="3%">删除</th>
                                        </tr>
                                        <?php foreach($newslist as $rs){; ?>
                                        <tr>
                                            <td><?php echo $rs['newsID']; ?></td>
                                            <td align="left"><a href=""></a><?php echo $rs['newsTitle']; ?></td>
                                            <td><?php echo $rs['newsAuthor']; ?></td>
                                            <td><?php echo $rs['newsSource']; ?></td>
                                            <td><?php echo date("Y-m-d H:i:s",$rs['newsSendTime']); ?></td>
                                            <td><?php echo date("Y-m-d H:i:s",$rs['newsUpdateTime']); ?></td>
                                            <td><?php echo $rs['newsHits']; ?></td>
                                            <td><?php if($row['isTop']==1){echo '是';}else{echo '否';};?></td>
                                            <td><?php if($row['isRed']==1){echo '是';}else{echo '否';};?></td>
                                            <td><a href="editNews.php?newsID=<?php echo $rs['newsID'] ?>"><img src="images/adj_bg.gif" width="15" height="15" border="0" /></a></td>
                                            <td><a href="delNews.php?newsID=<?php echo $rs['newsID']; ?>" onclick="return delNews();"><img src="images/del2.gif" width="16" height="16" border="0" /></a></td>
                                        </tr> 
                                        <?php };?>
                                        <tr>
                                            <td colspan="10" style="color:#0351a1; font-weight:normal; border:none">
                                                <?php echo"共有".$nums."篇文章";?>&nbsp;&nbsp; 
                                                <?php $page=new SubPages($page_size,$nums,$pageCurrent,$sub_pages,$subPage_link,2);?>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>   
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
